package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.system.Admin;
import com.qingcheng.service.system.AdminService;
import com.qingcheng.service.system.Login2Service;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDetailServiceImpl implements UserDetailsService {


    @Reference
    private AdminService adminService;

    @Reference
    private Login2Service login2Service;

    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("经过了UserDetailServiceImpl");

        List<Admin> list = new ArrayList<>();

        //从redis中拿分数,拿不到(null)就给个0
        Integer score = login2Service.findScore();
        if (score==null){
            score=0;
        }

        if( 10<score){
            Map map=new HashMap<>();
            map.put("loginName",s);
            map.put("status","1");
            list = adminService.findList(map);
            if(list.size()==0){
                return null;
            }

        }else {

            Map map=new HashMap<>();
            map.put("loginName",s);
            map.put("status","1");
            list = adminService.findList(map);
            list.get(0).setPassword("333333333333333");
            if(list.size()==0){
                return null;
            }
        }

        //删除缓存中分数
        login2Service.delScore();


        //实际项目中应该从数据库中提取用户的角色列表
        List<GrantedAuthority> grantedAuthorities=new ArrayList<GrantedAuthority>();
        grantedAuthorities.add( new SimpleGrantedAuthority("ROLE_ADMIN"));
        return new User(s,list.get(0).getPassword(),grantedAuthorities);


    }
}

